#include "stdio.h"

void led1on()
{
    printf("This is led1on\n");
}

void led1off()
{
    printf("This is led1off\n");
}

/**
 * @brief 比较两个字符串，返回 str1 - str2
 *
 * @param str1
 * @param str2
 * @return int
 */
int my_strcmp(const char *str1, const char *str2)
{
    while (*str1)
    {
        if (*str1 == *str2)
        {
            str1++;
            str2++;
            continue;
        }
        return *str1 - *str2;
    }

    return *str1 - *str2;
}

typedef struct cmd
{
    char *cmd_name;
    void (*pfunc)(void)
} _cmd;

/**
 * @brief 指令对应关系
 *
 */
_cmd cmd_list[] =
    {{"led1on", led1on},

     {"led1off", led1off}};


/**
 * @brief 遍历指令
 *
 * @param user_cmd
 * @return _cmd*
 */
_cmd *find_cmd(const char *user_cmd)
{
    for (size_t i = 0; i < sizeof(cmd_list) / sizeof(cmd_list[0]); i++)
    {
        if (my_strcmp(user_cmd, cmd_list[i].cmd_name) == 0)
        {
            return &(cmd_list[i]);
        }
    }
    return (_cmd *)0;
}

int main()
{
    _cmd *ptr = (_cmd *)0;
    const char *cmd_buf = "led1ofsafsafff";//模拟指令收到

    ptr = find_cmd(cmd_buf);

    if (ptr)//存在指令对应关系
    {
        ptr->pfunc(); //执行指令对应函数
    }
    else//不存在指令对应关系
    {
        printf("\r\n unknown command: ");
        printf(cmd_buf);
    }

    return 0;
}
